package de.lindenvalley.sevmob;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * This is main class that contains information about user's device
 * @author Lindenvalley GmbH
 *
 */
class SevMobDeviceInfo {
	
	private Context mContext;
	
	public SevMobDeviceInfo(Context context) {
		this.mContext = context;
	}
	
	/**
	 * the method returns MD5 of device ID
	 * @return MD5 of device ID
	 */
	public String getDeviceId(){
		TelephonyManager manager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceId = null;
		try{
			String result = manager.getDeviceId();
			deviceId = md5(result);
		} catch (Exception e) {
		}
		
		return deviceId;
	}
	
	/**
	 * The method returns String of device resolution like a WxH (for example 480x800)
	 * @return device resolution
	 */
	public String getScreenResolution(){
		WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		manager.getDefaultDisplay().getMetrics(outMetrics);
		return String.format("%dx%d", outMetrics.widthPixels, outMetrics.heightPixels);
	}
	
	/**
	 * The method returns device model name
	 * @return device model name
	 */
	public String getDeviceName(){
		return android.os.Build.MODEL;
		
	}
	
	/**
	 * The method returns fingerprint of device
	 * @return fingerprint of device
	 */
	public String getDeviceFingerprint(){
		return android.os.Build.FINGERPRINT;
	}
	
	
	
	private String md5(String s) {
	    try {
	        // Create MD5 Hash
	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	        digest.update(s.getBytes());
	        byte messageDigest[] = digest.digest();
	        
	        // Create Hex String
	        StringBuffer hexString = new StringBuffer();
	        for (int i=0; i<messageDigest.length; i++)
	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	        return hexString.toString();
	        
	    } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
	    }
	    return "";
	}
	

}
